package org.xawl.news.servlet.news;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.xawl.news.dao.impl.news.NewsDaoImpl;
import org.xawl.news.dao.news.NewsDao;
import org.xawl.news.entity.news.News;
import org.xawl.news.entity.user.User;
import org.xawl.news.servlet.base.BaseServlet;
import org.xawl.news.util.AjaxJson;
import org.xawl.news.util.SessionUtil;
import org.xawl.news.util.StringUtil;

import com.alibaba.fastjson.JSON;

public class NewsServlet extends BaseServlet {
	
	private static final long serialVersionUID = 1L;
	
	private NewsDao newsDao = new NewsDaoImpl();
	
	/**
	 * @Title: index
	 * @Description: 角色主页
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @return: String
	 */
	protected String list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<News> news = newsDao.findList();
		int count = newsDao.findCount();
		request.setAttribute("news", news);
		request.setAttribute("count", count);
		return "forward:news/newsList";
	}
	
	
	/**
	 * 
	 * @Title: add 
	 * @Description: 添加新闻
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @return: String
	 */
	protected String add(HttpServletRequest request, HttpServletResponse response) {
		return "forward:news/newsAdd";
	}
	
	protected void save(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		AjaxJson j = new AjaxJson();
		User currentUser = SessionUtil.getCurrentUser(request);
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		String state = request.getParameter("state");
		News news = new News();
		news.setTitle(title);
		news.setContent(content);
		news.setAuthor(currentUser.getUsername());
		news.setState(Integer.valueOf(state).intValue());
		news.setCreateTime(new Date());
		news.setUpdateTime(new Date());
		boolean flag = newsDao.save(news);
		if(flag) {
			j.setMsg("添加新闻成功!");
			j.setSuccess(true);
		} else {
			j.setMsg("添加新闻失败!");
			j.setSuccess(false);
		}
		response.getWriter().write(JSON.toJSONString(j));
	}
	
	
	/**
	 * 
	 * @Title: roleSave
	 * @Description: 新闻录入保存
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @return: String
	 */
	protected void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		AjaxJson j = new AjaxJson();
		String id = request.getParameter("id");
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		String state = request.getParameter("state");
		User currentUser = SessionUtil.getCurrentUser(request);
		if(StringUtil.isNotEmpty(id)) {
			 News news = newsDao.findById(Integer.valueOf(id).intValue());
			 news.setTitle(title);
			 news.setContent(content);
			 news.setAuthor(currentUser.getUsername());
			 news.setState(Integer.valueOf(state).intValue());
			 news.setUpdateTime(new Date());
			 boolean flag = newsDao.update(news);
			 if(flag) {
				 j.setMsg("更新新闻成功!");
				 j.setSuccess(true);
			 } else {
				j.setMsg("更新新闻失败!");
				j.setSuccess(false);
			 }
		} else {
			j.setMsg("新闻编号不能为空!");
			j.setSuccess(false);
		}
		response.getWriter().write(JSON.toJSONString(j));
	}
	
	/**
	 * 
	 * @Title: edit 
	 * @Description: 新闻编辑
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @return: String
	 */
	protected String edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id");
		News news = null;
		if(StringUtil.isNotEmpty(id)) {
			news = newsDao.findById(Integer.valueOf(id).intValue());
		}
		request.setAttribute("news", news);
		return "forward:news/newsEdit";
	}
	
	/**
	 * 
	 * @Title: delete 
	 * @Description: 新闻删除
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @return: String
	 */
	protected void del(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		AjaxJson j = new AjaxJson();
		String id = request.getParameter("id");
		if(StringUtil.isEmpty(id)) {
			j.setSuccess(false);
			j.setMsg("新闻编号不能为空!");
		} else {
			boolean flag = newsDao.delete(Integer.valueOf(id).intValue());
			if(flag) {
				j.setMsg("删除新闻成功!");
				j.setSuccess(true);
			} else {
				j.setMsg("删除新闻失败!");
				j.setSuccess(false);
			}
		}
		response.getWriter().write(JSON.toJSONString(j));
	}

}
